package com.mmy.controller.car;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.car.TCarOrg;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.impl.TCarOrgServiceImpl;

/**
 * 类的介绍：车公司信息
 *
 * @author shangzl
 * @date 创建日期：2020-08-06
 * @version 1.0
 */
@Controller
public class CarOrgController {

	@Autowired
	TCarOrgServiceImpl tCarOrgServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-06
	 */
	@RequestMapping(value = "/carorg")
	public String ToCarOrg(Model model) {
		try {

			List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
			model.addAttribute("carorglist", lstCarOrg);

			return "webbackground/car/carorg";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createcarorg")
	public String ToCreateCarOrg() {
		return "webbackground/car/createcarorg";
	}

	/**
	 * 函数功能说明： 新建车公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-06
	 */

	@RequestMapping(value = "/subcreatecarorg")
	public String CreateCarOrg(TCarOrg tCarOrg, HttpSession session) throws Exception {
//		获取最大值
		Object oOrgID = tCarOrgServiceImpl.getMaxOrgID();
//		 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 
		if (oOrgID == null) {
			tCarOrg.setOrgID(1);
		} else {
			tCarOrg.setOrgID((int) oOrgID + 1);
		}
		String strOrgName = tCarOrg.getOrgName();

		try {

//			 判断名称是否为空 
			if (strOrgName == "" || strOrgName == null) {
				return "test";
			} else {
//				 判断名称是否已存在 
				if (tCarOrgServiceImpl.findByOrgName(strOrgName).size() > 0) {

					return "test";
				} else {
					tCarOrg.setCreateDate(new Date());
					TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
					tCarOrg.setCreator(tSysUser.getUserName());
					tCarOrgServiceImpl.save(tCarOrg.getOrgID());
					tCarOrgServiceImpl.save(tCarOrg);
					return "redirect:carorg";

				}
			}

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除车公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@PostMapping("/delcarorg")
	@ResponseBody
	public String DelCarOrg(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstOrgID = new ArrayList<>();

		for (String str : checklist) {
			lstOrgID.add(Integer.parseInt(str));
		}

		tCarOrgServiceImpl.delCarOrg(lstOrgID);

		return "webbackground/car/carorg";
	}

	/**
	 * 函数功能说明： 修改车公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@RequestMapping(value = "/enterupdatecarorg")
	public String EnterUpdateCarOrg(HttpServletRequest request, Model model) {

		int iOrgID = Integer.parseInt(request.getParameter("iOrgID"));

		TCarOrg tCarOrg = tCarOrgServiceImpl.findByOrgID(iOrgID);
		model.addAttribute("tCarOrg", tCarOrg);
		return "webbackground/car/updatecarorg";

	}

	@RequestMapping(value = "/updatecarorg")
	public String UpdateCarOrg(TCarOrg tCarOrg, HttpSession session) throws Exception {
		try {

//			 定义一个新的实体类对象 
			TCarOrg dtCarOrg = tCarOrgServiceImpl.findByOrgID(tCarOrg.getOrgID());
			dtCarOrg.setOrgName(tCarOrg.getOrgName());
			dtCarOrg.setContact(tCarOrg.getContact());
			dtCarOrg.setMobile(tCarOrg.getMobile());
			dtCarOrg.setTel(tCarOrg.getTel());
			dtCarOrg.setFax(tCarOrg.getFax());
			dtCarOrg.setAddress(tCarOrg.getAddress());
			dtCarOrg.setNumber(tCarOrg.getNumber());
			dtCarOrg.setMemo(tCarOrg.getMemo());
			dtCarOrg.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarOrg.setReviser(tSysUser.getUserName());
			tCarOrgServiceImpl.save(dtCarOrg);
			return "redirect:carorg";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询车公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */
	@RequestMapping(value = "/searchcarorg")
	public String FindCarOrg(HttpServletRequest request, Model model) {

		String strOrgName = request.getParameter("OrgName");
		if (strOrgName == "") {
			List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
			model.addAttribute("carorglist", lstCarOrg);
			request.setAttribute("strOrgName", strOrgName);
			return "webbackground/car/carorg";
		} else {
			List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findByOrgName(strOrgName);
			model.addAttribute("carorglist", lstCarOrg);
			request.setAttribute("strOrgName", strOrgName);
			return "webbackground/car/carorg";
		}

	}
}
